package com.my.drools.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Description
 * @Author Godzilla
 * @Date 2019/1/28
 * @Version V1.0.0
 * @Update 更新说明
 */
public class XmlUtil {
    private static final Log LOGGER = LogFactory.getLog(XmlUtil.class);

    private XmlUtil() {
    }

    /**
     * 根据file获得Document对象
     *
     * @param file 文件
     * @return Document
     */
    public static Document getDocumentByFile(File file) {
        try {
            SAXReader reader = new SAXReader();
            return reader.read(file);
        } catch (Exception e) {
            throw new IllegalArgumentException("根据File获得Document对象异常", e);
        }
    }

    /**
     * 根据文件路径获得Document对象
     *
     * @param url 文件路径
     * @return Document
     */
    public static Document getDocumentByUrl(String url) {
        try {
            File file = new File(url);
            return getDocumentByFile(file);
        } catch (Exception e) {
            throw new IllegalArgumentException("根据Url获得Document对象异常", e);
        }
    }

    /**
     * 创建新的xml文件
     *
     * @param path     保存路径
     * @param document Document对象
     */
    public static void createXmlFile(String path, Document document) {
        XMLWriter writer = null;
        FileOutputStream outputStream = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    LOGGER.error("创建文件失败");
                    return;
                }
            }
            outputStream = new FileOutputStream(file);
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            writer = new XMLWriter(outputStream, format);
            writer.write(document);

        } catch (Exception e) {
            throw new IllegalArgumentException("创建xml文件异常", e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    LOGGER.error("关闭流失败", e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    LOGGER.error("关闭流失败", e);
                }
            }
        }
    }

    public static void writeXML(Document doc,File file) {

        //指定文件输出位置
        try {
            FileOutputStream out = new FileOutputStream(file);
            OutputFormat format = OutputFormat.createPrettyPrint();//标准化布局，适合查看时显示。
            format.setEncoding("utf-8");//指定文件格式
            XMLWriter writer = new XMLWriter(out, format);
            writer.write(doc);//写入文件
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
